<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" @click="handleAdd">添加代拍</el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      stripe
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="name" label="代拍名称" width="270" align="center" />
      <el-table-column prop="account" label="账号" width="180" align="center" />
      <el-table-column prop="password" label="密码" width="160" align="center" />
      <el-table-column label="操作" width="130" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-button size="mini" type="warning" icon="el-icon-edit" @click="handleEdit(scope.row)" />
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" center :visible.sync="dialogFormVisible">
      <el-form ref="form" :rules="rules" :model="temp" label-position="left" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model.trim="temp.name" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model.trim="temp.account" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input ref="password" v-model="temp.password" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button :loading="editLoading" type="primary" @click="handleSubmit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBidCos, updateBidCo } from '@/api/admin'

export default {
  name: 'Editor',
  data () {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得低于6位'))
      } else {
        callback()
      }
    }
    return {
      list: null,
      listLoading: true,
      editLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        edit: '修改账户',
        add: '添加代拍'
      },
      temp: {
        id: null,
        name: '',
        account: '',
        password: ''
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      }
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.listLoading = true
      getBidCos().then(res => {
        this.list = res.data
        this.listLoading = false
      }).catch(err => {
        this.listLoading = false
        console.log(err)
      })
    },
    resetTemp () {
      this.temp = {
        name: '',
        account: '',
        password: ''
      }
    },
    handleAdd () {
      this.resetTemp()
      this.dialogStatus = 'add'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleEdit (row) {
      this.dialogFormVisible = true
      this.temp = JSON.parse(JSON.stringify(row))
      this.dialogStatus = 'edit'
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleSubmit () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.editLoading = true
          updateBidCo(this.temp).then(res => {
            this.temp.id = res.data.id
            if (this.dialogStatus === 'add') {
              this.list.unshift(this.temp)
            } else {
              const index = this.list.findIndex(el => Number(el.id) === Number(res.data.id))
              this.list.splice(index, 1, this.temp)
            }
            this.dialogFormVisible = false
            this.editLoading = false
            this.$notify({
              message: res.msg,
              type: 'success',
              duration: 2000
            })
          }).catch(err => {
            this.editLoading = false
            console.log(err)
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-form {
  width: 100%;
  max-width: 450px;
  margin: auto;
}
</style>

